/*******************************************************************************
 * @(#)LogUtils.java 2015年8月26日
 *
 * Copyright 2015 emrubik Group Ltd. All rights reserved.
 * EMRubik PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *******************************************************************************/
package com.inossem.utils.log;

import android.content.Context;
import android.util.Log;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;

public class LogUtils {

    // Log.ERROR=6
    // Log.WARN=5
    // Log.INFO=4
    // Log.DEBUG=3
    // Log.VERBOSE=2
    private static final int LOG_LEVEL = 8; // 当LOG_LEVEL等于0时所有日志将不再输出

    private static final String TAG_INOSSEM = "INOSSEM";

    private static final String TAG_LOG = "LOG";

    private static final String NEW_LINE = "\n\n";

    /**
     * 输出Error级别日志
     *
     * @param message 输出信息
     * @author leij
     */
    public static void e(String message) {
        if (LOG_LEVEL > Log.ERROR) {
            Log.e(TAG_LOG, message);
        }
    }

    /**
     * 输出Error级别日志包含运行时异常信息
     *
     * @param message   输出信息
     * @param throwable 运行时异常类
     * @author leij
     */
    public static void e(String message, Throwable throwable) {
        if (LOG_LEVEL > Log.ERROR) {
            if (throwable == null) {
                Log.e(TAG_INOSSEM, message);
            } else {
                Log.e(TAG_INOSSEM, message, throwable);
            }

        }
    }

    public static void logE(Context context, String type, String message) {
        logE(context, type, message, null);
    }

    public static void logE(Context context, String type, String message, Throwable throwable) {
        saveLog(context, type, message + NEW_LINE + Utils.exceptionToString(throwable));
        e(message, throwable);
    }

    /**
     * 输出Warn级别日志包含运行时异常信息
     *
     * @param message   输出信息
     * @param throwable 运行时异常类
     * @author leij
     */
    private static void w(String message, Throwable throwable) {
        if (LOG_LEVEL > Log.WARN) {
            if (throwable == null) {
                Log.w(TAG_INOSSEM, message);
            } else {
                Log.w(TAG_INOSSEM, message, throwable);
            }
        }
    }

    public static void logW(Context context, String type, String message) {
        logW(context, type, message, null);
    }

    public static void logW(Context context, String type, String message, Throwable throwable) {
        saveLog(context, type, message + NEW_LINE + Utils.exceptionToString(throwable));
        w(message, throwable);
    }

    /**
     * 输出Info级别日志
     *
     * @param message 输出信息
     * @author leij
     */
    public static void i(String message) {
        if (LOG_LEVEL > Log.INFO) {
            Log.i(TAG_LOG, message);
        }
    }

    /**
     * 输出Info级别日志包含运行时异常信息
     *
     * @param message   输出信息
     * @param throwable 运行时异常类
     * @author leij
     */
    private static void i(String message, Throwable throwable) {
        if (LOG_LEVEL > Log.INFO) {
            if (throwable == null) {
                Log.i(TAG_INOSSEM, message);
            } else {
                Log.i(TAG_INOSSEM, message, throwable);
            }
        }
    }

    public static void logI(Context context, String type, String message) {
        logI(context, type, message, null);
    }

    public static void logI(Context context, String type, String message, Throwable throwable) {
        saveLog(context, type, message + NEW_LINE + Utils.exceptionToString(throwable));
        i(message, throwable);
    }

    /**
     * 输出Debug级别日志包含运行时异常信息
     *
     * @param message   输出信息
     * @param throwable 运行时异常类
     * @author leij
     */
    private static void d(String message, Throwable throwable) {
        if (LOG_LEVEL > Log.DEBUG) {
            if (throwable == null) {
                Log.d(TAG_INOSSEM, message);
            } else {
                Log.d(TAG_INOSSEM, message, throwable);
            }
        }
    }

    public static void logD(Context context, String type, String message) {
        logD(context, type, message, null);
    }

    public static void logD(Context context, String type, String message, Throwable throwable) {
        saveLog(context, type, message + NEW_LINE + Utils.exceptionToString(throwable));
        d(message, throwable);
    }

    /**
     * 输出Verbose级别日志包含运行时异常信息
     *
     * @param message   输出信息
     * @param throwable 运行时异常类
     * @author leij
     */
    private static void v(String message, Throwable throwable) {
        if (LOG_LEVEL > Log.VERBOSE) {
            if (throwable == null) {
                Log.v(TAG_INOSSEM, message);
            } else {
                Log.v(TAG_INOSSEM, message, throwable);
            }
        }
    }

    public static void logV(Context context, String type, String message) {
        logV(context, type, message, null);
    }

    public static void logV(Context context, String type, String message, Throwable throwable) {
        saveLog(context, type, message + NEW_LINE + Utils.exceptionToString(throwable));
        v(message, throwable);
    }

    /**
     * 保存指定类型日志
     *
     * @param type        日志类型
     * @param information 日志信息
     * @return 保存日志结果
     * @author leij
     */
    public static boolean saveLog(Context context, String type, String information) {
        return LogFile.saveLog(context, type, information);
    }

    /**
     * 获取日志根目录
     *
     * @return 保存根目录
     * @author leij
     */
    public static String getLogRootPath() {
        return LogFile.getRootPath();
    }


    public static void logThrowableE(Context context, String message, Throwable throwable) {
        logE(context, UtilsConstant.LOG_NAME_THROWABLE, message, throwable);
    }

    public static void logRequestE(Context context, String message, Throwable throwable) {
        logE(context, UtilsConstant.LOG_NAME_REQUEST, message, throwable);
    }

    public static void logRequestI(Context context, String message) {
        logI(context, UtilsConstant.LOG_NAME_REQUEST, message);
    }

    public static void logDatabaseE(Context context, String message, Throwable throwable) {
        logE(context, UtilsConstant.LOG_NAME_DATABASE, message, throwable);
    }

    public static void logDatabaseI(Context context, String message) {
        logI(context, UtilsConstant.LOG_NAME_DATABASE, message);
    }

    public static void logCatchExceptionE(Context context, String message, Throwable throwable) {
        logE(context, UtilsConstant.LOG_NAME_CRASHEXCEPTION, message, throwable);
    }

    public static void logQRCodeI(Context context, String message) {
        logI(context, UtilsConstant.LOG_NAME_QRCODE, message);
    }

}
